var Exercise=require('../models/exerciseModel.js');
var GuestPrePlan=require('../models/guestPrePlanModel.js');

var code=require('../txtCode/code.js');
var moment=require('moment');
var machineNumber=require('../txtCode/machineNumber.js');
var async=require('async');

var parseXlsx=require('excel');

module.exports={
	UpdateExercisePrePlan:function(req,res,next){
		if(req.session){
		
			var data=req.body;
			console.log(data);
			data.schedule=moment(data.schedule,moment.ISO_8601);
			console.log(typeof data.schedule);
			console.log(data.schedule);
			var guestname=data.guestname;
			var mobile=data.mobile;
			var prePlanname=data.prePlanname;
			var schedule=data.schedule;
			Exercise
			.update({guestname:guestname,
					mobile:mobile,
					prePlanname:prePlanname},
					{$set:{schedule:schedule}},function(err,result){
						if(err) console.log(err);
						console.log(result);
						res.send({
							code:[1000,'success'],
							data:'成功'
						});
					})
		};
	},//UpdateExErcisePrePlan()

	SaveExercise:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			console.log(data.schedule);
			console.log(typeof data.schedule);


			data.schedule=moment(data.schedule,moment.ISO_8601).subtract(5,'hours');
			console.log(typeof data.schedule);
			console.log(data.schedule);
			var oNewExercise=new Exercise(data);
		 	oNewExercise.save(function(err){

		 			if (err) console.log(err);
		 	
		 	}).then(
		 		function(data){
		 			res.send({code:code.success});	
		 			},
		 		function(err){
		 			console.log(err);
		 			res.send({code:code.insertFail});
			});//save()
		}//if(req.session)
	},//SaveExercise
	AddScheduleExercise:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			console.log(data.schedule);
			console.log(typeof data.schedule);


			data.schedule=moment(data.schedule,moment.ISO_8601).add(8,'hours').toDate();
			console.log(typeof data.schedule);
			console.log(data.schedule);
			var oNewExercise=new Exercise(data);
		 	oNewExercise.save(function(err){

		 			if (err) console.log(err);
		 	
		 	}).then(
		 		function(data){
		 			res.send({code:code.success});	
		 			},
		 		function(err){
		 			console.log(err);
		 			res.send({code:code.insertFail});
			});//save()
		}//if(req.session)
	},//AddScheduleExercise
	FetchOneDay:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			var year=parseInt(data.year);
			var month=parseInt(data.month);
			var day=parseInt(data.day);

			async.waterfall([
							function(cb){
								var mom=year+'-'+month+'-'+day+'T'+'07:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								    // mom=moment.utc([year,month,day,7,0,0,0]);
								console.log(typeof mom);
								console.log(mom);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,result)
								})
								// .count(function(err,count){
								// 	cb(null,count);
								// })
							},
							function(count7,cb){
								var mom=year+'-'+month+'-'+day+'T'+'08:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,cb){
								var mom=year+'-'+month+'-'+day+'T'+'09:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count);
								// 	// console.log(count7,count);
								// })
							}
							,
							function(count7,count8,count9,cb){
								var mom=year+'-'+month+'-'+day+'T'+'10:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,cb){
								var mom=year+'-'+month+'-'+day+'T'+'11:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,cb){
								var mom=year+'-'+month+'-'+day+'T'+'12:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,cb){
								var mom=year+'-'+month+'-'+day+'T'+'13:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,count12,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,cb){
								var mom=year+'-'+month+'-'+day+'T'+'14:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,cb){
								var mom=year+'-'+month+'-'+day+'T'+'15:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,cb){
								var mom=year+'-'+month+'-'+day+'T'+'16:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,cb){
								var mom=year+'-'+month+'-'+day+'T'+'17:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,cb){
								var mom=year+'-'+month+'-'+day+'T'+'18:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,cb){
								var mom=year+'-'+month+'-'+day+'T'+'19:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,
								count19,cb){
								var mom=year+'-'+month+'-'+day+'T'+'20:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,count19,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count19,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,
								count19,count20,cb){
								var mom=year+'-'+month+'-'+day+'T'+'21:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,count19,count20,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count19,count20,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,
								count19,count20,count21,cb){
								var mom=year+'-'+month+'-'+day+'T'+'22:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,count19,count20,count21,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count19,count20,count21,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,
								count19,count20,count21,count22,cb){
								var mom=year+'-'+month+'-'+day+'T'+'23:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,[count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,count19,count20,count21,count22,result])
								})
								// .count(function(err,count){
								// 	cb(null,[count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count19,count20,count21,
								// 		count22,count]);
								// 	// console.log(count7,count);
								// })
							},
							],function(err,result){
								if(err) console.log(err);
								console.log(result);
								res.send({
									code:[1000,'success'],
									data:result
								});
			});//async
		}//if(req.session)
	},//FetchOneDay
	FetchYuYueFormGuestnameO:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			Exercise
			.find({guestname:data.guestname})
			.then(function(data){
				console.log(data);
				res.send({
					code:[1000,'success'],
					data:data
				});
			},function(err){
				if(err) alert(err);
			})
		}//if(req.session)
	},//FetchYuYueFormGuestname
	FetchYuYueFormGuestname:function(req,res,next){
		if(req.session){
				var data=req.body;
				console.log(data);
				var guestname=data.guestname;
				var schedule=data.schedule
				Exercise
				.find({guestname:guestname,schedule:data.schedule})
				.then(function(data){
					console.log(data);
					var data=data[0];
					if(data.guestname){
							GuestPrePlan
							.find({
								guestname:data.guestname,
								prePlanname:data.prePlanname
							})
							.then(function(data){
								res.send({
									code:[1000,'success'],
									data:data[0]
								});
							});//Guestname
						}//if(data.guestname)
					// res.send({
					// 	code:[1000,'success'],
					// 	data:data[0]
					// })
				});
		}//if(req.session)
	},//FetchYuYueFormGuestname
	FetchYuYueFormSchedule:function(req,res,next){
		if(req.session){
				var data=req.body;
				console.log(data);
				var schedule=data.schedule;
				Exercise
				.find({schedule:data.schedule})
				.then(function(data){
					console.log(data);
					var containBox=new Array();
					

					// async.waterfall([function(cb){
					// 	data.forEach(function(v,i){
					// 	GuestPrePlan
					// 	.find({guestname:v.guestname})
					// 	.then(function(err,result){
					// 		containBox.concat(result);
					// 	});

					// 	}
					// }],function(err,result){

					// });//async.waterfall()
				
					// });//data.forEach()

					// GuestPrePlan
					// .find(function(gu))
					// async.waterfall([function(cb){

					// }],function(err,result){

					// });

					//async.parallel
					// var data=data[0];
					// if(data.guestname){
					// 		GuestPrePlan
					// 		.find({
					// 			guestname:data.guestname,
					// 			prePlanname:data.prePlanname
					// 		})
					// 		.then(function(data){
					// 			res.send({
					// 				code:[1000,'success'],
					// 				data:data[0]
					// 			});
					// 		});//Guestname
					// 	}//if(data.guestname)
					// res.send({
					// 	code:[1000,'success'],
					// 	data:data[0]
					// })
				});
		}
	},//FetchYuYueFormSchedule
	Population:function(req,res,next){
		if(req.session){
			
			///////////////////////////////////////////////////////////
			
			// var data=req.body;
			// console.log(data);
			// Exercise
			// .find({guestname:data.guestname})
			// .populate('guest')
			// .exec(function(err,result){
			// 	console.log(result);
			// 	res.send({
			// 		data:result[0].guest.prePlanMotions
			// 	});
			// });//Exercise()
			
			//////////////////////////////////////////////////////////

			//////////////////////////正式接口////////////////////////
			
			var data=req.body;
			console.log(data);
			Exercise
			.find({schedule:data.schedule})
			.populate('guest')
			.exec(function(err,result){
				console.log(result);
				res.send({
					data:result
				});
			});//Exercise()

			//////////////////////////////////////////////////////////

		};//if(req.session)
	},//Population
	FetchOneHour:function(req,res,next){
		if(req.session){
			var data=req.body.data;
			Exercise
			.find({schedule:data})
			.exec(function(err,result){
				if(err) console.log(err);
				res.send({
					data:result
				});
			})
		};//if(req.session)
	},//fetchOneHour() App 用
	VagueName:function(req,res,next){
		if(req.session){
			var data=req.body.vaguename;
			var regex=new RegExp(data);
   			Exercise
   			.find({'guestname':regex})
   			.exec(function(err,result){
   				if(err) console.log(err);
   				res.send({
   					code:[1000,'success'],
   					data:result
   				});
   			});//Exercise
		}//if(req.session)
	},//VagueName()
	ParseExcel:function(req,res,next){
	
		if(req.session){
			console.log(1);
			try{
			parseXlsx('test.xlsx',function(err,data){
				if(err) {
					console.log(err);
					return;
				}//if(err)
				console.log(data);
				res.send({data:data});
			});//
		}catch(e){
		if(e) console.log(e);			
		}
			
		};//if(req.session)
	},//ParseExcel
	ValidSchedule:function(req,res,next){
	
		async.waterfall(
			[
			  //当天
			  function(cb){
			  	var today=moment();
			  	console.log(today);
			  	var gteToday=today.subtract(5,'hours');
			  		today=moment();
			  	var ltToday=today.add(1,'days').subtract(5,'hours'); //注意前置自己会变化
			  	console.log(today);

			  	Exercise
					.find({
						schedule:{
							$gte:gteToday,
							$lt:ltToday
						}
					})
					.sort({
						schedule:1
					})
					.exec(function(err,docs){
							if(err){
								console.log(err);
									res.send({
											data:err
										});
								return;
							};//if(err)

							console.log(docs);
					
									today=moment();

								var year=today.year();
								var month=parseInt(today.month())+1;
								var day=parseInt(today.date());

								if(month<10){
									month=month+'';
									month='0'+month;
								}

								if(day<10){
									day=day+'';
									day='0'+day;
								}


								console.log('year:'+year);
								console.log('month:'+month);
								console.log('day:'+day);

							if(docs.length==0){
								
							

								var todayfrom=moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601);		
								var zeroToToday=todayfrom.add(1,'days').subtract(1,'hours');
								var todayfrom=moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601);		
								cb(null,{date:today.format(),
													time:
														[{from:todayfrom.format(),
														// [{from:1,
												    	 to:zeroToToday.format()
												    	}]
										});
							};//if(docs.length)

							if(docs.length>0){
								var row=parseInt(docs.length/6);
								var remainder=docs.length%6;
								console.log('remainder:'+remainder);
								var unitObject={};
									today=moment();

								    unitObject.date=today.format();

									unitObject.time=[];
							

								var qiantou=moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601);
								var houtou=moment(docs[0].schedule,moment.ISO_8601).subtract(8,'hours');
								console.log('houtou:');
								console.log(houtou);
								var qiantohou=houtou.diff(qiantou,'minutes');
								console.log('qiantohou:'+qiantohou);
								if(qiantohou>=59){
										
										unitObject.time.push({
											from:qiantou.format(),
											to:houtou.format()
										});
									}
								
							for( var i=0;i<=row;i++){
								console.log("i:"+i);
								var startDot=i*6;
								console.log(startDot);
								var endDot;
								if(i==row){
									endDot=0;
								}else{
									endDot=(i+1)*6;
								}

								console.log(endDot);

								var startHour=moment(docs[startDot].schedule,moment.ISO_8601).subtract(8,'hours');
								console.log(typeof startHour);
								console.log(startHour);
								console.log('startHour:'+startHour);
								var endHour;
									today=moment();
									year=today.year();
									year=year+'';
									month=parseInt(today.month())+1;
									day=parseInt(today.date());
									if(month<10){
										month=month+'';
										month='0'+month;
									}
									if(day<10){
										day=day+'';
										day='0'+day;
									}

								if(endDot==0){
									endHour=moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(1,'days');
								}else{
									endHour=moment(docs[endDot].schedule,moment.ISO_8601).add(5,'hours');
								};//if(endDot)

								console.log("endHour:"+endHour);
								
								console.log(endHour);

								var diff=endHour.diff(startHour,'minutes');
								console.log('两者的差距：'+endHour.diff(startHour,'minutes'));

								if(diff>=120){
									console.log(1);
									var durationStart=startHour.add(1,'hours');
									var durationEnd=endHour.subtract(1,'hours');
									console.log(durationStart);
									console.log(durationEnd);
									// var arrayUnit=new Array();
									// arrayUnit=[durationStart,durationEnd];
									// validSchedule.push(arrayUnit);
									// validSchedule.push({date:moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).subtract(5,'hours'),
														
									// 				   });
									// validSchedule[0].time.push({
									unitObject.time.push({
										from:durationStart.format(),
										to:durationEnd.format()
									});
									console.log(2);
								}

										if(i==row){
											cb(null,unitObject)
										};//if(i==row)
							};//for()
						};//if(>0)
					});//Exercise
			  },
			  //第二天
			  function(unitOne,cb){
			  	 		var finalArray=new Array();
			  	 		finalArray.push(unitOne);

			  	 		var today=moment();

						var year=today.year();
						var month=parseInt(today.month())+1;
						var day=parseInt(today.date());

						if(month<10){
							month=month+'';
							month='0'+month;
						}

						if(day<10){
							day=day+'';
							day='0'+day;
						}

						var unitObject={};
					        unitObject.date=moment(year+'-'+month+'-'+day+'T'+'00').add(1,'days').format();
							unitObject.time=[];

						var currentDay=moment(year+'-'+month+'-'+day+'T'+'00').add(1,'days');
						var nextDay=moment(year+'-'+month+'-'+day+'T'+'00').add(2,'days');
						Exercise
						.aggregate(
						{
							$match:{
							schedule:{
									$gte:moment(currentDay.format('L')).subtract(5,'hours').toDate(),
									$lt:moment(nextDay.format('L')).subtract(5,'hours').toDate()
								}//schedule
							}//$match
						},
						{
							$group:{
								_id:'$schedule',
								count:{
									$sum:1
								}
							}//$group
						}
						,
						function(err,docs){
							if(docs.length==0){
							docs=[0]};
							console.log('aggregate docs:'+docs);
							console.log('typeof docs[0][_id]:');
							console.log(typeof docs[0]._id);
							var oneDayArray=new Array(33);
								oneDayArray=[0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0]
							docs.forEach(function(v,i){
									var confireTime=moment(v._id,moment.ISO_8601).subtract(8,'hours');
									var confireHour=confireTime.hour();
									var confireMinute=confireTime.minute();
									
									if(confireMinute>10){
										var confireIndex=(confireHour-7)*2+1;
									}else{
										var confireIndex=(confireHour-7)*2;
									};//if(confireMinute>10)
										oneDayArray[confireIndex]=v.count;
									
									if(i==docs.length-1){
										
										//这里对于不同天
										var fanDay=moment().add(1,'days');
										var fanDayMonth=fanDay.month()+1;
										var fanDayYear=fanDay.year();
										var fanDayDay=fanDay.date();

										if(fanDayMonth<10){
											fanDayMonth=fanDayMonth+'';
											fanDayMonth='0'+fanDayMonth;
										};
										if(fanDayDay<10){
											fanDayDay=fanDayDay+'';
											fanDayDay='0'+fanDayDay;
										};
										console.log('oneDayArray:'+oneDayArray);
										oneDayArray.forEach(function(value,index){
											if(value>5){
												cb(null,'5678');
												return;
											}
											
											if(index==0 && (oneDayArray[index+1]+value)<6){
												console.log('这是7点');
												unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'07',moment.ISO_8601).format());
												console.log(unitObject.time);
											}
											if(0<index <31 &&
											   (oneDayArray[index-1]+value)<6 &&
											   (oneDayArray[index+1]+value)<6){
												if(index%2!=0){
													var fanHour=(index-1)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='30';
												}else{
													var fanHour=(index)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='00';
												};//if(index%2!=0)
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+fanHour+':'+fanMinute,moment.ISO_8601).format());		
											}
											if(index==32 && (oneDayArray[index-1]+value)<6){
													console.log('这是23点');
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'23'+':'+'00',moment.ISO_8601).format());
													console.log(unitObject.time);
													finalArray.push(unitObject);
													cb(null,finalArray);	
											};//
										});//oneDayArray.forEach（）
										
									};//if(i==docs.length-1)
							});//docs.forEach
						}
						)//Exercise()

			  },//function(unitOne,cb)
			  	function(finalArray,cb){

			  	 		var today=moment();

						var year=today.year();
						var month=parseInt(today.month())+1;
						var day=parseInt(today.date());

						if(month<10){
							month=month+'';
							month='0'+month;
						}

						if(day<10){
							day=day+'';
							day='0'+day;
						}

						var unitObject={};
							//不同的天
					        unitObject.date=moment(year+'-'+month+'-'+day+'T'+'00').add(2,'days').format();
					        //这里不同
							unitObject.time=[];

						//针对不同的天的查询
						var currentDay=moment(year+'-'+month+'-'+day+'T'+'00').add(2,'days');
						var nextDay=moment(year+'-'+month+'-'+day+'T'+'00').add(3,'days');
						//针对不同的天的查询
						Exercise
						.aggregate(
						{
							$match:{
							schedule:{
									$gte:moment(currentDay.format('L')).subtract(5,'hours').toDate(),
									$lt:moment(nextDay.format('L')).subtract(5,'hours').toDate()
								}//schedule
							}//$match
						},
						{
							$group:{
								_id:'$schedule',
								count:{
									$sum:1
								}
							}//$group
						}
						,
						function(err,docs){
							if(docs.length==0){
							docs=[0]};
							console.log('aggregate docs:'+docs);
							console.log('typeof docs[0][_id]:');
							console.log(typeof docs[0]._id);
							var oneDayArray=new Array(33);
								oneDayArray=[0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0]
							docs.forEach(function(v,i){
									var confireTime=moment(v._id,moment.ISO_8601).subtract(8,'hours');
									var confireHour=confireTime.hour();
									var confireMinute=confireTime.minute();
									
									if(confireMinute>10){
										var confireIndex=(confireHour-7)*2+1;
									}else{
										var confireIndex=(confireHour-7)*2;
									};//if(confireMinute>10)
										oneDayArray[confireIndex]=v.count;
									
									if(i==docs.length-1){
										
										//这里对于不同天
										var fanDay=moment().add(2,'days');
										var fanDayMonth=fanDay.month()+1;
										var fanDayYear=fanDay.year();
										var fanDayDay=fanDay.date();

										if(fanDayMonth<10){
											fanDayMonth=fanDayMonth+'';
											fanDayMonth='0'+fanDayMonth;
										};
										if(fanDayDay<10){
											fanDayDay=fanDayDay+'';
											fanDayDay='0'+fanDayDay;
										};
										console.log('oneDayArray:'+oneDayArray);
										oneDayArray.forEach(function(value,index){
											if(value>5){
												cb(null,'5678');
												return;
											}
											
											if(index==0 && (oneDayArray[index+1]+value)<6){
												console.log('这是7点');
												unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'07',moment.ISO_8601).format());
												console.log(unitObject.time);
											}
											if(0<index <31 &&
											   (oneDayArray[index-1]+value)<6 &&
											   (oneDayArray[index+1]+value)<6){
												if(index%2!=0){
													var fanHour=(index-1)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='30';
												}else{
													var fanHour=(index)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='00';
												};//if(index%2!=0)
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+fanHour+':'+fanMinute,moment.ISO_8601).format());		
											}
											if(index==32 && (oneDayArray[index-1]+value)<6){
													console.log('这是23点');
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'23'+':'+'00',moment.ISO_8601).format());
													console.log(unitObject.time);
													finalArray.push(unitObject);
													cb(null,finalArray);	
											};//
										});//oneDayArray.forEach（）
										
									};//if(i==docs.length-1)
							});//docs.forEach
						}
						)//Exercise()
			  	},//function(finalArray,cb)
			//第三天
			 	function(finalArray,cb){

			  	 		var today=moment();

						var year=today.year();
						var month=parseInt(today.month())+1;
						var day=parseInt(today.date());

						if(month<10){
							month=month+'';
							month='0'+month;
						}

						if(day<10){
							day=day+'';
							day='0'+day;
						}

						var unitObject={};
							//不同的天
					        unitObject.date=moment(year+'-'+month+'-'+day+'T'+'00').add(3,'days').format();
					        //这里不同
							unitObject.time=[];

						//针对不同的天的查询
						var currentDay=moment(year+'-'+month+'-'+day+'T'+'00').add(3,'days');
						var nextDay=moment(year+'-'+month+'-'+day+'T'+'00').add(4,'days');
						//针对不同的天的查询
						Exercise
						.aggregate(
						{
							$match:{
							schedule:{
									$gte:moment(currentDay.format('L')).subtract(5,'hours').toDate(),
									$lt:moment(nextDay.format('L')).subtract(5,'hours').toDate()
								}//schedule
							}//$match
						},
						{
							$group:{
								_id:'$schedule',
								count:{
									$sum:1
								}
							}//$group
						}
						,
						function(err,docs){
							if(docs.length==0){
							docs=[0]};
							console.log('aggregate docs:'+docs);
							console.log('typeof docs[0][_id]:');
							console.log(typeof docs[0]._id);
							var oneDayArray=new Array(33);
								oneDayArray=[0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0]
							docs.forEach(function(v,i){
									var confireTime=moment(v._id,moment.ISO_8601).subtract(8,'hours');
									var confireHour=confireTime.hour();
									var confireMinute=confireTime.minute();
									
									if(confireMinute>10){
										var confireIndex=(confireHour-7)*2+1;
									}else{
										var confireIndex=(confireHour-7)*2;
									};//if(confireMinute>10)
										oneDayArray[confireIndex]=v.count;
									
									if(i==docs.length-1){
										
										//这里对于不同天
										var fanDay=moment().add(3,'days');
										var fanDayMonth=fanDay.month()+1;
										var fanDayYear=fanDay.year();
										var fanDayDay=fanDay.date();

										if(fanDayMonth<10){
											fanDayMonth=fanDayMonth+'';
											fanDayMonth='0'+fanDayMonth;
										};
										if(fanDayDay<10){
											fanDayDay=fanDayDay+'';
											fanDayDay='0'+fanDayDay;
										};
										console.log('oneDayArray:'+oneDayArray);
										oneDayArray.forEach(function(value,index){
											if(value>5){
												cb(null,'5678');
												return;
											}
											
											if(index==0 && (oneDayArray[index+1]+value)<6){
												console.log('这是7点');
												unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'07',moment.ISO_8601).format());
												console.log(unitObject.time);
											}
											if(0<index <31 &&
											   (oneDayArray[index-1]+value)<6 &&
											   (oneDayArray[index+1]+value)<6){
												if(index%2!=0){
													var fanHour=(index-1)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='30';
												}else{
													var fanHour=(index)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='00';
												};//if(index%2!=0)
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+fanHour+':'+fanMinute,moment.ISO_8601).format());		
											}
											if(index==32 && (oneDayArray[index-1]+value)<6){
													console.log('这是23点');
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'23'+':'+'00',moment.ISO_8601).format());
													console.log(unitObject.time);
													finalArray.push(unitObject);
													cb(null,finalArray);	
											};//
										});//oneDayArray.forEach（）
										
									};//if(i==docs.length-1)
							});//docs.forEach
						}
						)//Exercise()
			  	},//function(finalArray,cb)

					//第四天
			 	function(finalArray,cb){

			  	 		var today=moment();

						var year=today.year();
						var month=parseInt(today.month())+1;
						var day=parseInt(today.date());

						if(month<10){
							month=month+'';
							month='0'+month;
						}

						if(day<10){
							day=day+'';
							day='0'+day;
						}

						var unitObject={};
							//不同的天
					        unitObject.date=moment(year+'-'+month+'-'+day+'T'+'00').add(4,'days').format();
					        //这里不同
							unitObject.time=[];

						//针对不同的天的查询
						var currentDay=moment(year+'-'+month+'-'+day+'T'+'00').add(4,'days');
						var nextDay=moment(year+'-'+month+'-'+day+'T'+'00').add(5,'days');
						//针对不同的天的查询
						Exercise
						.aggregate(
						{
							$match:{
							schedule:{
									$gte:moment(currentDay.format('L')).subtract(5,'hours').toDate(),
									$lt:moment(nextDay.format('L')).subtract(5,'hours').toDate()
								}//schedule
							}//$match
						},
						{
							$group:{
								_id:'$schedule',
								count:{
									$sum:1
								}
							}//$group
						}
						,
						function(err,docs){
							if(docs.length==0){
							docs=[0]};
							console.log('aggregate docs:'+docs);
							console.log('typeof docs[0][_id]:');
							console.log(typeof docs[0]._id);
							var oneDayArray=new Array(33);
								oneDayArray=[0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0]
							docs.forEach(function(v,i){
									var confireTime=moment(v._id,moment.ISO_8601).subtract(8,'hours');
									var confireHour=confireTime.hour();
									var confireMinute=confireTime.minute();
									
									if(confireMinute>10){
										var confireIndex=(confireHour-7)*2+1;
									}else{
										var confireIndex=(confireHour-7)*2;
									};//if(confireMinute>10)
										oneDayArray[confireIndex]=v.count;
									
									if(i==docs.length-1){
										
										//这里对于不同天
										var fanDay=moment().add(4,'days');
										var fanDayMonth=fanDay.month()+1;
										var fanDayYear=fanDay.year();
										var fanDayDay=fanDay.date();

										if(fanDayMonth<10){
											fanDayMonth=fanDayMonth+'';
											fanDayMonth='0'+fanDayMonth;
										};
										if(fanDayDay<10){
											fanDayDay=fanDayDay+'';
											fanDayDay='0'+fanDayDay;
										};
										console.log('oneDayArray:'+oneDayArray);
										oneDayArray.forEach(function(value,index){
											if(value>5){
												cb(null,'5678');
												return;
											}
											
											if(index==0 && (oneDayArray[index+1]+value)<6){
												console.log('这是7点');
												unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'07',moment.ISO_8601).format());
												console.log(unitObject.time);
											}
											if(0<index <31 &&
											   (oneDayArray[index-1]+value)<6 &&
											   (oneDayArray[index+1]+value)<6){
												if(index%2!=0){
													var fanHour=(index-1)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='30';
												}else{
													var fanHour=(index)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='00';
												};//if(index%2!=0)
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+fanHour+':'+fanMinute,moment.ISO_8601).format());		
											}
											if(index==32 && (oneDayArray[index-1]+value)<6){
													console.log('这是23点');
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'23'+':'+'00',moment.ISO_8601).format());
													console.log(unitObject.time);
													finalArray.push(unitObject);
													cb(null,finalArray);	
											};//
										});//oneDayArray.forEach（）
										
									};//if(i==docs.length-1)
							});//docs.forEach
						}
						)//Exercise()
			  	},//function(finalArray,cb)
			  			//第五天
			 	function(finalArray,cb){

			  	 		var today=moment();

						var year=today.year();
						var month=parseInt(today.month())+1;
						var day=parseInt(today.date());

						if(month<10){
							month=month+'';
							month='0'+month;
						}

						if(day<10){
							day=day+'';
							day='0'+day;
						}

						var unitObject={};
							//不同的天
					        unitObject.date=moment(year+'-'+month+'-'+day+'T'+'00').add(5,'days').format();
					        //这里不同
							unitObject.time=[];

						//针对不同的天的查询
						var currentDay=moment(year+'-'+month+'-'+day+'T'+'00').add(5,'days');
						var nextDay=moment(year+'-'+month+'-'+day+'T'+'00').add(6,'days');
						//针对不同的天的查询
						Exercise
						.aggregate(
						{
							$match:{
							schedule:{
									$gte:moment(currentDay.format('L')).subtract(5,'hours').toDate(),
									$lt:moment(nextDay.format('L')).subtract(5,'hours').toDate()
								}//schedule
							}//$match
						},
						{
							$group:{
								_id:'$schedule',
								count:{
									$sum:1
								}
							}//$group
						}
						,
						function(err,docs){
							if(docs.length==0){
							docs=[0]};
							console.log('aggregate docs:'+docs);
							console.log('typeof docs[0][_id]:');
							console.log(typeof docs[0]._id);
							var oneDayArray=new Array(33);
								oneDayArray=[0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0]
							docs.forEach(function(v,i){
									var confireTime=moment(v._id,moment.ISO_8601).subtract(8,'hours');
									var confireHour=confireTime.hour();
									var confireMinute=confireTime.minute();
									
									if(confireMinute>10){
										var confireIndex=(confireHour-7)*2+1;
									}else{
										var confireIndex=(confireHour-7)*2;
									};//if(confireMinute>10)
										oneDayArray[confireIndex]=v.count;
									
									if(i==docs.length-1){
										
										//这里对于不同天
										var fanDay=moment().add(5,'days');
										var fanDayMonth=fanDay.month()+1;
										var fanDayYear=fanDay.year();
										var fanDayDay=fanDay.date();

										if(fanDayMonth<10){
											fanDayMonth=fanDayMonth+'';
											fanDayMonth='0'+fanDayMonth;
										};
										if(fanDayDay<10){
											fanDayDay=fanDayDay+'';
											fanDayDay='0'+fanDayDay;
										};
										console.log('oneDayArray:'+oneDayArray);
										oneDayArray.forEach(function(value,index){
											if(value>5){
												cb(null,'5678');
												return;
											}
											
											if(index==0 && (oneDayArray[index+1]+value)<6){
												console.log('这是7点');
												unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'07',moment.ISO_8601).format());
												console.log(unitObject.time);
											}
											if(0<index <31 &&
											   (oneDayArray[index-1]+value)<6 &&
											   (oneDayArray[index+1]+value)<6){
												if(index%2!=0){
													var fanHour=(index-1)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='30';
												}else{
													var fanHour=(index)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='00';
												};//if(index%2!=0)
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+fanHour+':'+fanMinute,moment.ISO_8601).format());		
											}
											if(index==32 && (oneDayArray[index-1]+value)<6){
													console.log('这是23点');
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'23'+':'+'00',moment.ISO_8601).format());
													console.log(unitObject.time);
													finalArray.push(unitObject);
													cb(null,finalArray);	
											};//
										});//oneDayArray.forEach（）
										
									};//if(i==docs.length-1)
							});//docs.forEach
						}
						)//Exercise()
			  	},//function(finalArray,cb)
			  		//第六天
			 	function(finalArray,cb){

			  	 		var today=moment();

						var year=today.year();
						var month=parseInt(today.month())+1;
						var day=parseInt(today.date());

						if(month<10){
							month=month+'';
							month='0'+month;
						}

						if(day<10){
							day=day+'';
							day='0'+day;
						}

						var unitObject={};
							//不同的天
					        unitObject.date=moment(year+'-'+month+'-'+day+'T'+'00').add(6,'days').format();
					        //这里不同
							unitObject.time=[];

						//针对不同的天的查询
						var currentDay=moment(year+'-'+month+'-'+day+'T'+'00').add(6,'days');
						var nextDay=moment(year+'-'+month+'-'+day+'T'+'00').add(7,'days');
						//针对不同的天的查询
						Exercise
						.aggregate(
						{
							$match:{
							schedule:{
									$gte:moment(currentDay.format('L')).subtract(5,'hours').toDate(),
									$lt:moment(nextDay.format('L')).subtract(5,'hours').toDate()
								}//schedule
							}//$match
						},
						{
							$group:{
								_id:'$schedule',
								count:{
									$sum:1
								}
							}//$group
						}
						,
						function(err,docs){
							if(docs.length==0){
							docs=[0]};
							console.log('aggregate docs:'+docs);
							console.log('typeof docs[0][_id]:');
							console.log(typeof docs[0]._id);
							var oneDayArray=new Array(33);
								oneDayArray=[0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0,0,0,0,0,0,0,0,
										     0,0,0]
							docs.forEach(function(v,i){
									var confireTime=moment(v._id,moment.ISO_8601).subtract(8,'hours');
									var confireHour=confireTime.hour();
									var confireMinute=confireTime.minute();
									
									if(confireMinute>10){
										var confireIndex=(confireHour-7)*2+1;
									}else{
										var confireIndex=(confireHour-7)*2;
									};//if(confireMinute>10)
										oneDayArray[confireIndex]=v.count;
									
									if(i==docs.length-1){
										
										//这里对于不同天
										var fanDay=moment().add(6,'days');
										var fanDayMonth=fanDay.month()+1;
										var fanDayYear=fanDay.year();
										var fanDayDay=fanDay.date();

										if(fanDayMonth<10){
											fanDayMonth=fanDayMonth+'';
											fanDayMonth='0'+fanDayMonth;
										};
										if(fanDayDay<10){
											fanDayDay=fanDayDay+'';
											fanDayDay='0'+fanDayDay;
										};
										console.log('oneDayArray:'+oneDayArray);
										oneDayArray.forEach(function(value,index){
											if(value>5){
												cb(null,'5678');
												return;
											}
											
											if(index==0 && (oneDayArray[index+1]+value)<6){
												console.log('这是7点');
												unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'07',moment.ISO_8601).format());
												console.log(unitObject.time);
											}
											if(0<index <31 &&
											   (oneDayArray[index-1]+value)<6 &&
											   (oneDayArray[index+1]+value)<6){
												if(index%2!=0){
													var fanHour=(index-1)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='30';
												}else{
													var fanHour=(index)/2+7;
													if(fanHour<10){
														fanHour=fanHour+'';
														fanHour='0'+fanHour;
													}
													var fanMinute='00';
												};//if(index%2!=0)
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+fanHour+':'+fanMinute,moment.ISO_8601).format());		
											}
											if(index==32 && (oneDayArray[index-1]+value)<6){
													console.log('这是23点');
													unitObject.time.push(moment(fanDayYear+'-'+fanDayMonth+'-'+
																				fanDayDay+'T'+'23'+':'+'00',moment.ISO_8601).format());
													console.log(unitObject.time);
													finalArray.push(unitObject);
													cb(null,finalArray);	
											};//
										});//oneDayArray.forEach（）
										
									};//if(i==docs.length-1)
							});//docs.forEach
						}
						)//Exercise()
			  	},//function(finalArray,cb)
			],function(err,docs){
				if(err){
					console.log(err);
					res.send({
						data:err
					});
					return;
				};//if(err)

				console.log('docs 类型');
				console.log(typeof docs);

				res.send({
					code:[1000,'success'],
					data:docs
				});
		});//async

	},//ValidSchedule
	SearchGuestExercise:function(req,res,next){
		if(req.session){
			

			var needMobile=req.body.needMobile;

			var regex=new RegExp(needMobile);

			// Guest
			// .find()
			// .or([{guestname:regex},{mobile:regex}])

			Exercise
			.aggregate(
				{
					$match:
					{
						$or:
							[{guestname:regex},{mobile:regex}]
					}
				},
				{
					$group:{
						_id:'$mobile',
						guestname:{$first:"$guestname"},
						content:{
							$push:{
								schedule:"$schedule",	
								prePlanname:'$prePlanname'
							}
						},

					}
				},function(err,docs){
					if(err){
						console.log(err);
						res.send({
							code:[1001,'service is wrong'],
							data:err	
						});
						return;
					};//if(err)
					
					res.send({
						code:[1000,'success'],
						data:docs
					});//res.send
				}//function(err,docs)
				);
		};//if(req.session)
	}//SearchGuestExercise
}